/**
 * 简单数组求差值，数据元素可以为string | number | null | undefined，值可以为NaN
 * @param arr1
 * @param arr2
 */
export function diffSimpleArray<T = string | number | null | undefined>(arr1: T[], arr2: T[]) {
  return arr1.concat(arr2).filter((v) => {
    return !arr1.includes(v) || !arr2.includes(v)
  })
}

/**
 * 简化数组项
 * @param treeData
 * @param fn 简化方法
 */
export function simplifyData<T extends any, U extends any>(treeData: T[], fn: (t: T) => U): U[] {
  return treeData.map((t: T) => {
    const children = t.children;
    let result: U = fn(t);
    if (Array.isArray(children) && children.length !== 0) {
      result.children = simplifyData(children, fn)
    }
    return result
  });
}

/**
 * 平铺数组项的某一个属性
 * @param treeData
 * @param key
 * @param result
 */
export function flatByProps(treeData: any[], key: string, result: any = []) {
  treeData.forEach((value) => {
    result.push(value[key]);
    if (Array.isArray(value.children) && value.children.length !== 0) {
      flatByProps(value.children, key, result)
    }
  });
  return result
}

